import {GlobalEnv} from "../utils/GlobalEnv";
import {ConfigKey} from "../constant/ConfigConstant";
import {UnitController} from "../controller/UnitController";
import {DataRouter} from "../frame/dataSource/DataRouter";
import {SpecialEffectType} from "../type/Type";
import {TaskController} from "../controller/TaskController";
import Configer from "../frame/apiModel/config/Configer";
import {UnitType} from "../frame/dataSource/DataModel";
import {DataRouterUtil} from "../utils/router/DataRouterUtil";
import {TimeController} from "../controller/TimeController";
import {FinalKey} from "../constant/FinalConstant";
import {TriggerFactory} from "../frame/TriggerFactory";
import {UnitModel} from "../frame/apiModel/adapterModel/model3/UnitModel";
import {PlayerModel} from "../frame/apiModel/adapterModel/model4/PlayerModel";
import {ModelFactory} from "../frame/apiModel/adapterModel/model5/ModelFactory";
import {ResourceKey} from "../constant/ResourceConstant";

export function unitDeadTrigger() {
    const deadCallBack = function (this: void, killer: UnitModel, killed: UnitModel, killerPlayer: PlayerModel, killedPlayer: PlayerModel) {

        const killerIsPlayer = killerPlayer?.isNotEndPlaying();
        const killedIsPlayer = killedPlayer?.isNotEndPlaying();
        if (killerIsPlayer) {
            if (killedIsPlayer) {
            } else {
                GlobalEnv.addKillNum(killerPlayer);
            }
        }

        const isAttackMonster =  GlobalEnv.removeAllAttackMonster(killed);
        if (isAttackMonster) {
            const num = GlobalEnv.getAllAttackMonsterNum();
            if (num === 0) {
                const callBacks = GlobalEnv.nextWaveCallBack;
                for (let i = 0; i < callBacks.length; i++) {
                    const callBack = callBacks[i];
                    callBack();
                }
                callBacks.length = 0;
            }
        }


        const bMonster = UnitController.unitItem_generateData_Map.has(killed);
        if (bMonster) {
            const monsterData = UnitController.unitItem_generateData_Map.get(killed);
            UnitController.getInstance().clearGenerateData(killed);
            UnitController.getInstance().checkToInitMonster(monsterData, 1, true);
        }
        // 单位死亡后无论是什么单位都要移除附加属性，否则内存泄漏
        // 记录单位击杀数量
        if (killer?.isValid && killer?.isValid()) {
            TaskController.getInstance().dropFunction(killer, killed);
        }

        UnitController.getInstance().killUnit(killer, killed);

        const strId = killed.strId;
        const unitData = DataRouter.getUnitDataByUnitId(strId);


        if (unitData) {
            const type = unitData.unit_type;
            if (type === UnitType.boss) {
                // 目标点特效
                const point_effects: string = Configer.getResource(ResourceKey.bossDeadEffect);
                const point_effects_time = 3;
                let loc = killed.loc;
                if (point_effects) {
                    TimeController.addSpecialEffectFun(killer, point_effects, point_effects_time, loc, undefined, SpecialEffectType.point, 0)
                }

                // const bossArr: UnitModel[] = [];
                // const allMap = Configer.getFinal(FinalKey.allMapRect);
                // ModelFactory.getInstance().mapUnitInRect(allMap, (unit) => {
                //     const strId = unit.strId;
                //     const unitData = DataRouter.getUnitDataByUnitId(strId);
                //     if (unitData) {
                //         const type = unitData.unit_type;
                //         if (type === UnitType.boss) {
                //             bossArr.push(unit);
                //         }
                //     }
                // })
                //
                // for (let i = 0; i < bossArr.length; i++) {
                //     const boss = bossArr[i];
                //     DataRouterUtil.setMonsterAttrByBoss(boss, bossArr.length);
                // }
            } else {
                const point_effects: string = Configer.getResource(ResourceKey.normalDeadEffect);
                const point_effects_time = 3;
                let loc = killed.loc;
                if (point_effects) {
                    TimeController.addSpecialEffectFun(killer, point_effects, point_effects_time, loc, undefined, SpecialEffectType.point, 0)
                }
            }
        }
    }
    TriggerFactory.getInstance().registerUnitDead(deadCallBack)
}




// todo 开启无尽模式
function startHentai () {

}